﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace AspDIY.Net.Common
{

    /// <summary>
    /// 分页类
    /// </summary>
    public partial class BuildPager : System.Web.UI.Page
    {
        #region 基本定义

        #region 总记录数
        int _TotalRecords = 0;
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalRecords
        {
            get { return _TotalRecords; }
            set { _TotalRecords = value; }
        }
        #endregion

        #region 当前页
        int _CurrentPage = 1;
        /// <summary>
        /// 当前页 默认第一页
        /// </summary>
        public int CurrentPage
        {
            get { return _CurrentPage; }
            set { _CurrentPage = value; }
        }
        #endregion

        #region 每页记录数
        int _PageSize = 10;
        /// <summary>
        /// 每页记录数 默认10
        /// </summary>
        public int PageSize
        {
            get { return _PageSize; }
            set { _PageSize = value; }
        }
        #endregion

        #region Url
        string _Url = "";
        /// <summary>
        /// url后面参数
        /// </summary>
        public string Url
        {
            get { return _Url; }
            set { _Url = value; }
        }
        #endregion

        #region 开始页
        int _StartPage = 1;
        /// <summary>
        ///开始页 默认第1页
        /// </summary>
        public int StartPage
        {
            get { return _StartPage; }
            set { _StartPage = value; }
        }
        #endregion

        #region 中间页
        int _Alter = 5;
        /// <summary>
        /// 中间页 默认5
        /// </summary>
        public int Alter
        {
            get { return _Alter; }
            set { _Alter = value; }
        }
        #endregion

        #region 数字分页样式
        string _StrNum = "<a href=\"{0}p={1}\">{2}</a>　";
        /// <summary>
        /// 数字分页样式 ＜a href=\"{0}p={1}\"＜{2}＜/a＞　动态＜a href=\"{0}{1}.shtml\"＞{2}＜/a＞　静态
        /// </summary>
        public string StrNum
        {
            get { return _StrNum; }
            set { _StrNum = value; }
        }
        #endregion

        #region 首页样式
        string _StrFirst = "首页";
        /// <summary>
        /// 首页样式 默认 首页
        /// </summary>
        public string StrFirst
        {
            get { return _StrFirst; }
            set { _StrFirst = value; }
        }
        #endregion

        #region 上一页样式
        string _StrPrev = "上一页";
        /// <summary>
        /// 上一页样式 默认上一页
        /// </summary>
        public string StrPrev
        {
            get { return _StrPrev; }
            set { _StrPrev = value; }
        }
        #endregion

        #region 当前页样式
        string _StrCurr = "<font color=red>{0}</font>　";
        /// <summary>
        /// 当前页样式 默认＜font color=red＞{0}＜/font＞
        /// </summary>
        public string StrCurr
        {
            get { return _StrCurr; }
            set { _StrCurr = value; }
        }
        #endregion

        #region 下一页样式
        string _StrNext = "下一页";
        /// <summary>
        /// 下一页样式 默认下一页
        /// </summary>
        public string StrNext
        {
            get { return _StrNext; }
            set { _StrNext = value; }
        }
        #endregion

        #region 尾页样式
        string _StrLast = "尾页";
        /// <summary>
        /// 尾页样式 默认尾页
        /// </summary>
        public string StrLast
        {
            get { return _StrLast; }
            set { _StrLast = value; }
        }
        #endregion

        #region 共N条样式
        string _StrCount = "共{0}条　";
        /// <summary>
        /// 共N条样式 默认 共{0}条
        /// </summary>
        public string StrCount
        {
            get { return _StrCount; }
            set { _StrCount = value; }
        }
        #endregion

        #region 第N页/共N页样式
        string _StrPage = "第{0}页/共{1}页　";
        /// <summary>
        /// 第N页/共N页样式 默认 第{0}页/共{1}页
        /// </summary>
        public string StrPage
        {
            get { return _StrPage; }
            set { _StrPage = value; }
        }
        #endregion

        #region 跳转控件
        string _StrTurn = "转到 <input name=\"Page\" type=\"text\" id=\"Page\" size=5 onkeydown=\"if (event.keyCode == 13){{Utils.GoToPage({0},'{1}p=');}}\"> 页";
        /// <summary>
        /// 跳转控件 转到 (input控件)页
        /// </summary>
        public string StrTurn
        {
            get { return _StrTurn; }
            set { _StrTurn = value; }
        }
        #endregion

        #region 是否显示总记录数
        bool _BolCount = true;
        /// <summary>
        /// 是否显示总记录数 默认true
        /// </summary>
        public bool BolCount
        {
            get { return _BolCount; }
            set { _BolCount = value; }
        }
        #endregion

        #region 是否显示第N页/共N页
        bool _BolPage = true;
        /// <summary>
        /// 是否显示第N页/共N页 默认true
        /// </summary>
        public bool BolPage
        {
            get { return _BolPage; }
            set { _BolPage = value; }
        }
        #endregion

        #region 是否显示首页
        bool _BolFirst = true;
        /// <summary>
        /// 是否显示首页 默认true
        /// </summary>
        public bool BolFirst
        {
            get { return _BolFirst; }
            set { _BolFirst = value; }
        }
        #endregion

        #region 是否显示尾页
        bool _BolLast = true;
        /// <summary>
        /// 是否显示尾页 默认true
        /// </summary>
        public bool BolLast
        {
            get { return _BolLast; }
            set { _BolLast = value; }
        }
        #endregion

        #region 是否显示跳转
        bool _BolTurn = true;
        /// <summary>
        /// 是否显示跳转 默认true
        /// </summary>
        public bool BolTurn
        {
            get { return _BolTurn; }
            set { _BolTurn = value; }
        }
        #endregion

        #region 是否显示数字分页
        bool _BolNum = true;
        /// <summary>
        /// 是否显示数字分页 默认true
        /// </summary>
        public bool BolNum
        {
            get { return _BolNum; }
            set { _BolNum = value; }
        }
        #endregion

        #region 计算总页数
        /// <summary>
        /// 计算总页数
        /// </summary>
        private int CalculateTotalPages()
        {
            int totalPagesAvailable;

            totalPagesAvailable = TotalRecords / PageSize;

            //由于C#的整形除法 会把所有余数舍入为0，所以需要判断是否需要加1
            if ((TotalRecords % PageSize) > 0)
                totalPagesAvailable++;

            return totalPagesAvailable;
        }
        #endregion

        #endregion

        #region 创建并显示分页器
        /// <summary>
        /// 创建并显示分页器
        /// </summary>
        public string PageStr()
        {
            int totalPages = CalculateTotalPages();
            if (CurrentPage > totalPages) CurrentPage = totalPages;
            int endPage = CurrentPage + Alter;
            if (totalPages > 0)
            {
                StringBuilder sb = new StringBuilder("");
                if (CurrentPage > Alter) StartPage = CurrentPage - Alter;

                if (endPage > totalPages) endPage = totalPages;

                if (BolCount) sb.AppendFormat(StrCount, TotalRecords);
                if (BolPage) sb.AppendFormat(StrPage, CurrentPage, totalPages);
                if (BolFirst && CurrentPage > 1) sb.AppendFormat(_StrNum, Url, 1, StrFirst);

                if (CurrentPage != StartPage) sb.AppendFormat(_StrNum, Url, CurrentPage - 1, StrPrev);
                if (BolNum)
                {
                    for (int i = StartPage; i <= endPage; i++)
                    {
                        if (CurrentPage == i)
                        {
                            sb.AppendFormat(StrCurr, i);
                        }
                        else
                        {
                            sb.AppendFormat(_StrNum, Url, i,  i );
                        }
                    }
                }

                if (CurrentPage != endPage) sb.AppendFormat(_StrNum, Url, CurrentPage + 1, StrNext);
                if (BolLast && CurrentPage < totalPages) sb.AppendFormat(_StrNum, Url, totalPages, StrLast);
                if (BolTurn)
                {
                    sb.AppendFormat(StrTurn, totalPages, Url);
                }
                return sb.ToString();
            }
            else
            {
                return "";
            }
        }
        #endregion

    }
}
